using System.Collections;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Core;
using UnityEditor;
using UnityEngine;

namespace XFGameFramework
{
    public class AssetNameTools
    {


        /// <summary>
        /// 获取资源名称
        /// </summary>
        /// <param name="name">预期资源名称</param>
        /// <param name="forder">资源所在目录</param>
        /// <returns></returns>
        public static string GetName(string name, string forder)
        { 
            if (!AssetDatabase.IsValidFolder(forder))
                return name;

            string tempName = name;
             
            int count = 0;

            while (IsContainName(tempName,forder))
            {
                count++; 
                tempName = string.Format("{0} ({1})", name, count); 
            }

            return tempName;
        }

        private static bool IsContainName(string name, string forder) 
        {
            string[] matchs = AssetDatabase.FindAssets(name, new string[] { forder });

            foreach (var item in matchs)
            {
                string asset_path = AssetDatabase.GUIDToAssetPath(item);
                if (string.IsNullOrEmpty(asset_path)) continue;
                if (System.IO.Path.GetFileNameWithoutExtension(asset_path) == name)
                    return true;
            }

            return false;
        }


    }

}

